package storm.Processors;

import java.util.Arrays;

import storm.Tasks.Task;

/***********************************************/
class Memory {
	 reg tab[];
	
	Memory(int size) {
		this.tab = new reg[size];
		for (int i =0 ; i <size; i ++){
			 this.tab[i] = new reg(-1,null);
		}
		
	}
	
	String getCarto(){
		String res = "\n/***************************************************************/\n";
		int cum =1;
		Task pred = this.tab[0].T;
		int id = -1;
		for (int i =1 ; i <this.tab.length; i ++){
			id = -1;
			if ( this.tab[i].T == pred){
				cum ++;
			}
			else{
			
				if (pred != null ) id = pred.id; 
				res = res + id + "  " + cum +"\n"; 
				cum =1;
				pred =  this.tab[i].T;
			}
			
		}
		if (pred != null ) id = pred.id; 
		res = res + id + "  " + cum +"\n"; 
		
		res = res + "/***************************************************************/\n";
		return res;
	}
	
	int getUsed(Task T){
		
		int res = 0;
		for (int i =0 ; i < this.tab.length ; i++) {
			if (tab[i].T==T) res ++;
		}
		return res;
		
	}


	
  void addLine(Task t ,int s, int date){
	if (s>this.tab.length) {
		  System.out.println("[CACHE SIZE TOO SMALL -------- ]");
		  return ;
		  
	  }
	  
	
	Arrays.sort( this.tab);
	 for (int i =0 ; s>0 ; i++) {
		 	if (tab[i].T != t) {
		 		this.tab[i] = new reg(date,t);
		 		s--;
		 	}
	 }
	Arrays.sort( this.tab);
	 
 }

public void maj(Task t, int  d, int date) {
	Arrays.sort( this.tab);
	for (int i =0 ; d>0 ; i++) {
	 	if (tab[i].T == t) {
	 		this.tab[i] = new reg(date,t);
	 		d--;
	 	}
	}
	Arrays.sort( this.tab);
	// TODO Auto-generated method stub
	
}


}